using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class pages_TourBooking : System.Web.UI.Page
{
    /*
     * This page publish a p_booking session variable which save tour package booking information
     * of customer. 
     * This variable will be used by CheckoutTourBooking.aspx page
     */
    PackageBooking pb = new PackageBooking();

    protected void Page_Load(object sender, EventArgs e)
    {
        TourDAO daoTour = new TourDAO();
        int id = int.Parse(Request["id"]);
        pb.Tour = daoTour.GetTour(id);
        lblTourName.Text = pb.Tour.Name;
        lblPrice.Text = Convert.ToString(pb.Tour.Price - pb.Tour.Discount);
        Session["p_booking"] = pb;
        uctPersonInfo.LoadData();
    }

    protected void lnkAccept_Click(object sender, EventArgs e)
    {
        
        Validate();
        if (!IsValid) return;
        GetPersonInfo();
        User user = Session["user"] as User;
        if (user == null) //if error occur, system can get customer information
        {
            lblMessage.Text = "Sorry. Error occured when tryinging to create your booking in our system.<br>Please try later or contact us";
            return;

        }
        if (IsRegisteredUser(user))
        {            
            AddBookingForRegisteredUser(user);
            
        }
        else
        {

            user.PersonInfor.ModifiedDate = DateTime.Now;
            AddBookingForUnregisterUser(user.PersonInfor);

        }
    }
    //Get personal information in registration form
    public void GetPersonInfo()
    {
        User user = Session["user"] as User;
        if (user == null) user = new User();
        user.PersonInfor.FirstName = uctPersonInfo.FirstName;
        user.PersonInfor.LastName = uctPersonInfo.LastName;
        user.PersonInfor.DateOfBirth = uctPersonInfo.DateOfBirth;
        user.PersonInfor.Email = uctPersonInfo.Email;
        user.PersonInfor.Gender = uctPersonInfo.Gender;
        user.PersonInfor.Phone = uctPersonInfo.Phone;
        user.PersonInfor.Country = uctPersonInfo.Country;
        user.PersonInfor.State = uctPersonInfo.State;
        user.PersonInfor.City = uctPersonInfo.City;
        user.PersonInfor.Address = uctPersonInfo.Address;
        Session["user"] = user;
    }

    /// <summary>
    /// Check if a user is regestered user
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    public bool IsRegisteredUser(User user)
    {
        return user.Id > 0;
    }
    /// <summary>
    /// Add tour booking for registered customer
    /// </summary>
    /// <param name="userId"></param>
    public void AddBookingForRegisteredUser(User user)
    {
        BookingDAO daoBooking=new BookingDAO();
        PackageBookingDAO daoPackageBooking = new PackageBookingDAO();      
        
        if (pb.ID <= 0) pb = Session["p_booking"] as PackageBooking;
        if (pb == null)
        {
            lblMessage.Text = "Error";
            return;
        }
        pb.CustomerID = user.PersonInfor.Id;
        pb.DepartureTime = DateTime.Parse(txtDepartureDate.Text);
        pb.GuestNumber = int.Parse(txtGuestNumber.Text);        
        pb.Price = pb.Tour.Price - pb.Tour.Discount;
        pb.Type = 1;
        pb.ModifiedDate = DateTime.Now;
        pb.ModifiedBy = user.Id;
        if (daoBooking.Insert(pb) > 0)
        {
            if (daoPackageBooking.Insert(pb) > 0)
            {
                Session["p_booking"] = pb;
                Response.Redirect("CheckoutTourBooking.aspx");
            }
            else lblMessage.Text = "Sorry! Some errors has ocurred in our system.<br/> Please try later or contact us now";
        }
        else lblMessage.Text = "Sorry! Some errors has ocurred in our system.<br/> Please try later or contact us now";
        
    }
    
    /// <summary>
    /// Add tour booking for unregister customer
    /// </summary>
    /// <param name="perInfo"></param>
    public void AddBookingForUnregisterUser(Person perInfo)
    {
        PersonDAO daoPerson=new PersonDAO();
        BookingDAO daoBooking=new BookingDAO();
        PackageBookingDAO daoPackageBooking=new PackageBookingDAO();
        

        pb.DepartureTime = DateTime.Parse(txtDepartureDate.Text);
        pb.GuestNumber = int.Parse(txtGuestNumber.Text);
        pb.Note = txtNote.Text;
        pb.Type = 1;
        if (daoPerson.Insert(perInfo) <= 0)
        {
            lblMessage.Text = "Error";
            return;
        }
        pb.ModifiedDate = DateTime.Now;
        pb.CustomerID = perInfo.Id;
        pb.Type = 1;
        if (daoBooking.Insert(pb) <= 0)
        {
            lblMessage.Text = "Error when create new booking";
            return;
        }
        if (daoPackageBooking.Insert(pb) <= 0)
        {
            lblMessage.Text = "Error when trying to create package booking";
            return;
        }
        Session["p_booking"] = pb;
        Response.Redirect("CheckoutTourBooking.aspx");
    }

    /// <summary>
    /// Cancel current booking and try another one
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkChange_Click(object sender, EventArgs e)
    {
        GetPersonInfo();
        Response.Redirect("Default.aspx");
    }
}
